import axios from "axios";

//请求拦截
axios.interceptors.request.use((request) => {
    // 设置基础 URL
    // request.baseURL = "http://10.10.233.103:18888/p4java"
    request.baseURL = "http://localhost:18888/p4java"
    // request.baseURL = "http://192.168.1.210:18888/p4java"
    // request.baseURL = "http://192.168.0.201:18888/p4java"
    // 设置请求头
    request.headers = {
        "content-type": "application/json;charset=utf-8"
    }
    return request;
}, (error) => {
    //请求出错处理逻辑
    return Promise.reject(error)
});


//响应拦截
axios.interceptors.response.use((response) => {
    /**
     * 将 data 数据返回,项目中 this.axios的 then 获取的数据就是 data ,我们需要的真实数据了
     */
    let data = response.data;
    // 在项目中与后端协同开发,定义的响应码等都可以在这里设置

    return data;
}, (error) => {
    //响应出错处理逻辑
    let httpCode = error.response.status;
    let message;
    if (httpCode === 200) {
        //响应出错处理逻辑
        let code = error.response.data.code;
        let msg = error.response.data.msg;
        switch (code) {
            case 10000:
                message = msg;
                break;
            case 50000:
                message = '未知异常! 请联系管理员';
                break;
            case 50002:
                message = msg;
                break;
            case 50003:
                message = msg;
                break;
            case 50004:
                message = msg;
                break;
        }
    } else {
        switch (httpCode) {
            case 504:
                message = "网关超时,请稍等片刻重试";
                break;
            default:
                message = '未知异常! 网站正在维护...';
                break;
        }
    }
    window.vm.$Message.error({
        background: true,
        content: message,
        duration: 5,
        closable: true
    });
    return Promise.reject(error)
});